﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class FormTransfer : Form
    {
        public FormTransfer()
        {
            InitializeComponent();
            
        }

        public FormTransfer(Transfer trans)
        {
            InitializeComponent();
            this.Text = Forms.TRANSFER_FORM;
            
            if (trans == null)
            {
                transfer = new Transfer();
                transfer.Status = TransferState.New;
                transfer.SalonRec = Global.currentUser.Salon;
                transfer.Id = 0;
                txtSalonRec.Text = Global.currentUser.Salon.Name;
                setStatus(transfer);
            }
            else
                fillFields(trans);
        }

        private Transfer transfer;

        private void FormTransfer_Load(object sender, EventArgs e)
        {

        }

        private void fillFields(Transfer trans)
        {
            this.transfer = trans;
            this.txtCar.SelectedText = trans.Car.Id.ToString();
            this.txtSalonRec.Text = trans.SalonRec.Name;
            this.txtSalonSend.Text = trans.SalonSen.Name;
            setStatus(trans);
            
        }

        private void setStatus(Transfer tr)
        {
            switch (tr.Status)
            { 
                case TransferState.Ordered:
                    this.lblStatus.Text = Global.GetEnumDescription(TransferState.Ordered);
                    this.btnOrderReceive.Visible = false;
                    this.btnSend.Visible = true;
                    this.btnCancel.Visible = true;
                    this.dtpOrderDate.Enabled=false;
                    this.dtpOrderDate.Value = tr.OrderDate.Date;
                    this.dtpShipCancelDate.Enabled=true;
                    this.dtpReceiveDAte.Value = DateTime.MinValue.AddYears(1969);
                    this.dtpReceiveDAte.Enabled=false;
                    break;
                case TransferState.Send:
                    this.lblStatus.Text = Global.GetEnumDescription(TransferState.Send);
                    this.btnOrderReceive.Visible = true;
                    this.btnOrderReceive.Text = "Zapisz odbiór";
                    this.btnSend.Visible = false;
                    this.btnCancel.Visible = false;
                    this.dtpOrderDate.Enabled = false;
                    this.dtpOrderDate.Value = tr.OrderDate.Date;
                    this.dtpShipCancelDate.Enabled = false;
                    this.dtpShipCancelDate.Value = tr.ShipCancelDate.Date;
                    this.dtpReceiveDAte.Value = DateTime.Now;
                    this.dtpReceiveDAte.Enabled = true;
                    break;
                case TransferState.Cancel:
                    this.lblStatus.Text = Global.GetEnumDescription(TransferState.Cancel);
                    this.btnOrderReceive.Visible = false;
                    this.btnSend.Visible = false;
                    this.btnCancel.Visible = false;
                    this.dtpOrderDate.Enabled = false;
                    this.dtpOrderDate.Value = tr.OrderDate.Date;
                    this.dtpShipCancelDate.Enabled = false;
                    this.dtpShipCancelDate.Value = tr.ShipCancelDate.Date;
                    this.dtpReceiveDAte.Enabled = false;
                    this.dtpReceiveDAte.Value = tr.ReceiveDate.Date;
                    break;
                case TransferState.Received:
                    this.lblStatus.Text = Global.GetEnumDescription(TransferState.Received);
                    this.btnOrderReceive.Visible = false;
                    this.btnSend.Visible = false;
                    this.btnCancel.Visible = false;
                    this.dtpOrderDate.Enabled = false;
                    this.dtpOrderDate.Value = tr.OrderDate.Date;
                    this.dtpShipCancelDate.Enabled = false;
                    this.dtpShipCancelDate.Value = tr.ShipCancelDate.Date;
                    this.dtpReceiveDAte.Enabled = false;
                    this.dtpReceiveDAte.Value = tr.ReceiveDate.Date;
                    break;
                case TransferState.New:
                    this.lblStatus.Text = Global.GetEnumDescription(TransferState.New);
                    this.btnOrderReceive.Visible = true;
                    this.btnSend.Visible = false;
                    this.btnCancel.Visible = false;
                    this.dtpOrderDate.Enabled = true;
                    this.dtpShipCancelDate.Enabled = false;
                    this.dtpOrderDate.Value = DateTime.Now;
                    this.dtpReceiveDAte.Enabled = false;
                    this.dtpShipCancelDate.Value = DateTime.MinValue.AddYears(1969);
                    this.dtpReceiveDAte.Value = DateTime.MinValue.AddYears(1969);
                    break;
                default:
                    break;
            }
            refereshButtons();
        }

        //podwójne kliknięcie otwiera rejestr samochodów w sposób modalny
        //aktywny jest wtedy przycisk Wybierz
        //Przekazanie wybranego samochodu z rejestru do tej formatki 
        //następuje przez pole ChosenCar które trzeba dodać w formatce Rejestru Samochodów
        private void txtCar_DoubleClick(object sender, EventArgs e)
        {
            if (transfer.Id == 0)
            {
                RegisterCars regcar = new RegisterCars(true);
                regcar.ShowDialog();
                if (regcar.ChosenCar != null)
                {
                    transfer.Car = regcar.ChosenCar;
                    txtCar.Text = transfer.Car.Id.ToString();
                    txtSalonSend.Text = transfer.Car.Salon.Name;
                }
            }

        }

        private void btnCarDetails_Click(object sender, EventArgs e)
        {
            Global.MainPage.openNewCarForm(transfer.Car);
        }

        private void btnOrderReceive_Click(object sender, EventArgs e)
        {
            TransferService ts = ServiceFactory.getTransferService();
            if (transfer.Status == TransferState.New)
            { 
                if (ts.checkIfCarIsTRansfered(transfer.Car))
                {
                    MessageBox.Show("Ten samochód jest w trakcie transferu");
                    return;
                }
                transfer.SalonSen = transfer.Car.Salon;
                transfer.OrderDate = dtpOrderDate.Value;
                transfer.ReceiveDate = dtpReceiveDAte.Value;
                transfer.ShipCancelDate = dtpShipCancelDate.Value;
                transfer.Status = TransferState.Ordered;
                ts.saveTransfer(transfer);
                MessageBox.Show("Transfer samochodu został zapisany");
                this.Close();
                this.Dispose();
            }
            else if (transfer.Status == TransferState.Send)
            {
                Car car = transfer.Car;
                car.Salon = transfer.SalonRec;
                ServiceFactory.getCarService().saveCar(car);

                transfer.ReceiveDate = dtpReceiveDAte.Value;
                transfer.Status = TransferState.Received;

                ServiceFactory.getTransferService().saveTransfer(transfer);
                MessageBox.Show("Odbiór samochodu został zapisany");
                this.Close();
                this.Dispose();

            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            TransferService ts = ServiceFactory.getTransferService();
            transfer.ShipCancelDate = dtpShipCancelDate.Value;
            transfer.Status = TransferState.Send;
            ts.saveTransfer(transfer);
            MessageBox.Show("Wysyłka samochodu została zapisana");
            this.Close();
            this.Dispose();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            TransferService ts = ServiceFactory.getTransferService();
            transfer.ShipCancelDate = dtpShipCancelDate.Value;
            transfer.Status = TransferState.Cancel;
            ts.saveTransfer(transfer);
            MessageBox.Show("Anulowano transfer samochodu");
            this.Close();
            this.Dispose();
        }

        private void refereshButtons()
        {
            if (transfer.Id > 0 && !transfer.SalonRec.Equals(Global.currentUser.Salon) && !transfer.SalonSen.Equals(Global.currentUser.Salon))
            {
                btnCancel.Visible = false;
                btnOrderReceive.Visible = false;
                btnSend.Visible = false;
            }
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            txtCar_DoubleClick(sender, e);
        }
    }
}
